﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using System.Threading;
using System.Globalization;
using University.Workshop.DataAccess.LanguagePack.ClientLanguagePack;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Interaction logic for selectCar.xaml
    /// </summary>
    public partial class selectCar : Window
    {
        User user;
        Client client = new Client();
        string myCar;
        long amount;
        public string Parameter { get; set; }
        littleRace race = null;
        public bool Banned = false;
        string language;
        public selectCar(User received,string languageReceived)
        {
            
            InitializeComponent();
            language = languageReceived;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
            ApplyLanguage();
            textAmount.Text = "0";
            user = received;
            labelChips.Content = user.Chips;
        }

        private void ApplyLanguage()
        {
            label1.Content = ClientLanguagePack.SelectCarSelect;
            label2.Content = ClientLanguagePack.SelectCarYouHave;
            label3.Content = ClientLanguagePack.SelectCarBet;
            button1.Content = ClientLanguagePack.SelectCarButtonAccept;
            label4.Content = ClientLanguagePack.SelectCarMax;
        }
        private string validation(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return ("There is no an amount");
            }
            else
            {
                if (text.Contains(" "))
                {
                    return ("There is a space, remove it");
                }

                else
                {
                    amount = Convert.ToInt64(text);
                    if (amount > user.Chips)
                    {
                        return ("You don´t have " + amount + " chips");
                    }
                    else
                    {


                        return ("OK");
                    }
                }

            }
        }

        private void check()
        {
            
            long number;
            bool a= Int64.TryParse(textAmount.Text,out number);
            if (a)
            {
                labelMessage.Content = validation(textAmount.Text);
                string label = Convert.ToString(labelMessage.Content);
                if (label == "OK")
                {
                    user.Chips = user.Chips - amount;
                    if (radioBlue.IsChecked == true)
                        myCar = "carBlue";
                    if (radioGreen.IsChecked == true)
                        myCar = "carGreen";
                    if (radioPink.IsChecked == true)
                        myCar = "carPink";
                    if (radioRed.IsChecked == true)
                        myCar = "carRed";
                    if (radioYellow.IsChecked == true)
                        myCar = "carYellow";
                    race = new littleRace(user, myCar, amount,language);
                    race.ShowDialog();
                    this.Close();
                }
            }
            else {
                labelMessage.Content="Invalid argument";
            }
            
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
                this.Parameter = "Races";
                if (!client.GetGameStatus(user, this.Parameter))
                {
                    MessageBox.Show("you are played today, try again tomorrow");
                    this.Close();
                }
                else
                    check();
            
            
        }

        private void textAmount_TextChanged(object sender, TextChangedEventArgs e)
        {
            labelMessage.Content = "";
        }

        private void textAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                check();
            }
            if (e.Key >= Key.D0 && e.Key <= Key.D9 || e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9||e.Key==Key.Decimal)
                e.Handled = false;
            else
                e.Handled = true;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (Banned)
            {
                race.Banned = true;
                race.Close();
            }
        }
        
        internal void Ban()
        {
            Banned = true;
            Close();
        }
    }
}
